﻿using System;
using System.Web.UI.WebControls;
using CivilRegistry.ControlledModel;
using CivilRegistry.Networking;

namespace CivilRegistry.Common
{
    public static class SMSHelper
    {
        public static void SendApplicationRequestNotification(string applicationType, User user, ref Label lblStatus)
        {
            try
            {
                SMSEngine smsEngine = new SMSEngine();
                smsEngine.PhoneNumber = user.PhoneNo;
                smsEngine.Message = Messages.SMSMessage.ApplicationSent.Replace("<ApplicationType>", applicationType);
                if (smsEngine.Send())
                {
                    lblStatus.Text += " You will receive a sms shortly with application status.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the sms due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the sms due to an internal error.";
            }
        }

        public static void SendApproveSMS(string applicationType, User user, ref Label lblStatus)
        {
            try
            {
                SMSEngine smsEngine = new SMSEngine();
                smsEngine.PhoneNumber = user.PhoneNo;
                smsEngine.Message = Messages.SMSMessage.ApplicationApproved.Replace("<ApplicationType>", applicationType);
                if (smsEngine.Send())
                {
                    lblStatus.Text += " Also, an sms has been sent to " + user.FirstName + " with application status.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the sms due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the sms due to an internal error.";
            }
        }

        public static void SendDeactivationSMS(string applicationType, string deactivationButtonText, User user, ref Label lblStatus)
        {
            try
            {
                SMSEngine smsEngine = new SMSEngine();
                smsEngine.PhoneNumber = user.PhoneNo;
                if (deactivationButtonText == "Re-Activate")
                {
                    smsEngine.Message = Messages.SMSMessage.ApplicationReActivated.Replace("<ApplicationType>", applicationType);
                }
                else
                {
                    smsEngine.Message = Messages.SMSMessage.ApplicationDeactivated.Replace("<ApplicationType>", applicationType);
                }
                if (smsEngine.Send())
                {
                    lblStatus.Text += " Also, an sms has been sent to " + user.FirstName + " with application status.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the sms due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the sms due to an internal error.";
            }
        }

        public static void SendDenyNotificationSMS(string applicationType, User user, ref Label lblStatus)
        {
            try
            {
                SMSEngine smsEngine = new SMSEngine();
                smsEngine.PhoneNumber = user.PhoneNo;
                smsEngine.Message = Messages.SMSMessage.ApplicationDenyed.Replace("<ApplicationType>", applicationType);
                if (smsEngine.Send())
                {
                    lblStatus.Text += " Also, an sms has been sent to " + user.FirstName + " with application status.";
                }
                else
                {
                    lblStatus.Text += " Unable to send the sms due to an internal error.";
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorLogger.Log(ex);
                lblStatus.Text += " Unable to send the sms due to an internal error.";
            }
        }
    }
}